<?php

namespace Helpers;

use Helpers\calendar;


    class Gantti {

        var $cal = null;
        var $data = array();
        var $first;
        var $last;
        var $options = array();
        var $cellstyle = false;
        var $blocks = array();
        var $months = array();
        var $days = array();
        var $desc = array();
        var $seconds = 0;
        var $data2 = array();

        function setData2($data2) {
            $this->data2 = $data2;

            ;
        }

        function findSelected($selected) {


            if ($selected == "Te gjitha") {

                return $this;
            }

            $i = 0;
            $regularIndex = 0;
            $dataLocal = array();
            foreach ($this->blocks as $i => $block) {

                if ($block['label'] == $selected) {


                    $dataLocal[$regularIndex] = $this->data[$i];
                    $regularIndex++;
                }

                $i++;
            }
            $gantti = new Gantti($dataLocal, array(
                'title' => 'Makinat',
                'cellwidth' => 20,
                'cellheight' => 20,
                'today' => true
            ));
            return $gantti;
        }

        function __construct($data, $params = array()) {

            $defaults = array(
                'title' => false,
                'cellwidth' => 40,
                'cellheight' => 40,
                'today' => true,
            );

            $this->options = array_merge($defaults, $params);
            $this->cal = new Calendar();
            $this->data = $data;
            $this->seconds = 60 * 60 * 24;

            $this->cellstyle = 'style="width: ' . $this->options['cellwidth'] . 'px; height: ' . $this->options['cellheight'] . 'px"';

            // parse data and find first and last date

            $this->parse();
        }

        function parse() {


            foreach ($this->data as $d) {

                $this->blocks[] = array(
                    'label' => $d['label'],
                    'start' => $start = strtotime($d['start']),
                    'end' => $end = strtotime($d['end']),
                    'class' => @$d['class'],
                    'desc' => @$d['desc']
                );

                if (!$this->first || $this->first > $start)
                    $this->first = $start;
                if (!$this->last || $this->last < $end)
                    $this->last = $end;
            }

            $this->first = $this->cal->date($this->first);
            $this->last = $this->cal->date($this->last);

            $current = $this->first->month();
            $lastDay = $this->last->month()->lastDay()->timestamp;

            // build the months
            while ($current->lastDay()->timestamp <= $lastDay) {
                $month = $current->month();
                $this->months[] = $month;
                foreach ($month->days() as $day) {
                    $this->days[] = $day;
                }
                $current = $current->next();
            }
        }

        function render() {

            $html = array();

            // common styles
            $cellstyle = 'style="line-height: ' . $this->options['cellheight'] . 'px; height: ' . $this->options['cellheight'] . 'px"';
            $wrapstyle = 'style="width: ' . $this->options['cellwidth'] . 'px"';
            $totalstyle = 'style="width: ' . (count($this->days) * $this->options['cellwidth']) . 'px"';
            // start the diagram
            $html[] = '<figure class="gantt">';

            // set a title if available
            if ($this->options['title']) {
                $html[] = '<figcaption>' . $this->options['title'] . '</figcaption>';
            }

            // sidebar with labels
            $html[] = '<aside>';
            $html[] = '<ul class="gantt-labels" style="margin-top: ' . (($this->options['cellheight'] * 2) + 1) . 'px">';

            $once2 = false;
            $once22 = false;
            $dataOnce2['label'] = "klevis";



            foreach ($this->blocks as $i => $block) {
                if ($dataOnce2['label'] != $block['label']) {

                    $once2 = true;
                    $html[] = '<li class="gantt-label"><strong id= \'' . $block['label'] . '\' ' . $cellstyle . '>' . $block['label'] . '</strong></li>';

                    $dataOnce2['label'] = $block['label'];
                } else {
                    
                }

                if ($once2 == true and $once22 == false) {
                    $dataOnce2['label'] = $block['label'];
                    $once22 = true;
                }
            }

            foreach ($this->data2 as $j => $dataUn) {
                $html[] = '<li class="gantt-label"><strong id= \'' . $dataUn['label'] . '\' ' . $cellstyle . '>' . $dataUn['label'] . '</strong></li>';
            }


            $html[] = '</ul>';
            $html[] = '</aside>';

            // data section
            $html[] = '<section id="data" class="gantt-data">';

            // data header section
            $html[] = '<header>';

            // months headers
            $html[] = '<ul class="gantt-months" ' . $totalstyle . '>';
            foreach ($this->months as $month) {
                $html[] = '<li class="gantt-month" style="width: ' . ($this->options['cellwidth'] * $month->countDays()) . 'px"><strong ' . $cellstyle . '>' . $month->name() . '</strong></li>';
            }
            $html[] = '</ul>';

            // days headers
            $html[] = '<ul class="gantt-days" ' . $totalstyle . '>';
            foreach ($this->days as $day) {

                $weekend = ($day->isWeekend()) ? ' weekend' : '';
                $today = ($day->isToday()) ? ' today' : '';

                $html[] = '<li id=\'' . $day . '\' class="gantt-day' . $weekend . $today . '" ' . $wrapstyle . '><span ' . $cellstyle . 'id=\'' . $day . '\'  >' . $day->padded() . '</span></li>';
            }
            $html[] = '</ul>';

            // end header
            $html[] = '</header>';

            // main items
            $html[] = '<ul class="gantt-items" ' . $totalstyle . '>';

            $once = false;
            $dataOnce['label'] = "klevis";
            $once21 = false;


            foreach ($this->blocks as $i => $block) {



                if ($dataOnce['label'] != $block['label']) {
                    $html[] = '</li>';
                    $once = true;
                    $html[] = '<li class="gantt-item">';

                    // days
                    $html[] = '<ul class="gantt-days">';
                    foreach ($this->days as $day) {

                        $weekend = ($day->isWeekend()) ? ' weekend' : '';
                        $today = ($day->isToday()) ? ' today' : '';

                        $html[] = '<li id=' . $day . ' class="gantt-day' . $weekend . $today . '" ' . $wrapstyle . '><span ' . $cellstyle . '>' . $day . '</span></li>';
                    }
                    $html[] = '</ul>';

                    // the block
                    $days = (($block['end'] - $block['start']) / $this->seconds);
                    $offset = (($block['start'] - $this->first->month()->timestamp) / $this->seconds);
                    $top = round($i * ($this->options['cellheight'] + 1));
                    $left = round($offset * $this->options['cellwidth']);
                    $width = round(($days + 1) * $this->options['cellwidth'] - 9);
                    $height = round($this->options['cellheight'] - 8);
                    $class = ($block['class']) ? ' ' . $block['class'] : '';

                    $myDateStart = $block['start'];
                    $myDateEnd = $block['end'];
                    $myDateStart = date("Y-m-d", $myDateStart);
                    $myDateEnd = date("Y-m-d", $myDateEnd);

                    $check_date = $myDateStart;
                    $dateIntervalSHow = "";
                    $dateIntervalSHow = $dateIntervalSHow . '--> d' . $check_date;

                    $i = 0;
                    while ($check_date != $myDateEnd) {
                        $check_date = date("Y-m-d", strtotime("+1 day", strtotime($check_date)));
                        $dateIntervalSHow = $dateIntervalSHow . '--> d' . $check_date;

                        $i++;

                        if ($i > 31) {
                            break;
                        }
                    }





                    $html[] = '<a     id=\'[' . $block['label'] . ']' . $dateIntervalSHow . '  href=\'\'
			  class="gantt-block' . $class . '" style="left: ' . $left . 'px; width: ' . $width . 'px; height: ' . $height . 'px"      >';



                    $html[] = '<strong title=\'' . $block['desc'] . '\' class="gantt-block-label">' . '</strong>';
                    $html[] = '</a>';
                    $dataOnce['label'] = $block['label'];
                } else {



                    $days = (($block['end'] - $block['start']) / $this->seconds);
                    $offset = (($block['start'] - $this->first->month()->timestamp) / $this->seconds);
                    $top = round($i * ($this->options['cellheight'] + 1));
                    $left = round($offset * $this->options['cellwidth']);
                    $width = round(($days + 1) * $this->options['cellwidth'] - 9);
                    $height = round($this->options['cellheight'] - 8);
                    $class = ($block['class']) ? ' ' . $block['class'] : '';

                    $myDateStart = $block['start'];
                    $myDateEnd = $block['end'];
                    $myDateStart = date("Y-m-d", $myDateStart);
                    $myDateEnd = date("Y-m-d", $myDateEnd);



                    $check_date = $myDateStart;
                    $dateIntervalSHow = $dateIntervalSHow . '--> d' . $check_date;


                    $i = 0;
                    while ($check_date != $myDateEnd) {
                        $check_date = date("Y-m-d", strtotime("+1 day", strtotime($check_date)));
                        $dateIntervalSHow = $dateIntervalSHow . '--> d' . $check_date;

                        $i++;
                        if ($i > 31) {
                            break;
                        }
                    }






                    $html[] = '<a     id=\'[' . $block['label'] . ']' . $dateIntervalSHow . '    href=\'\'
			  class="gantt-block' . $class . '" style="left: ' . $left . 'px; width: ' . $width . 'px; height: ' . $height . 'px"      >';



                    $html[] = '<strong title=\'' . $block['desc'] . '\' class="gantt-block-label">' . '</strong>';
                    $html[] = '</a>';
                }

                if ($once == true and $once21 == false) {
                    $dataOnce['label'] = $block['label'];
                    $once21 = true;
                }
            }

            foreach ($this->data2 as $j => $dataUn) {
                $html[] = '<ul class="gantt-days">';
                foreach ($this->days as $day) {

                    $weekend = ($day->isWeekend()) ? ' weekend' : '';
                    $today = ($day->isToday()) ? ' today' : '';

                    $html[] = '<li id=' . $day . ' class="gantt-day' . $weekend . $today . '" ' . $wrapstyle . '><span ' . $cellstyle . '>' . $day . '</span></li>';
                }
                $html[] = '</ul>';
            }



            $html[] = '</ul>';

            if ($this->options['today']) {

                // today
                $today = $this->cal->today();

                $offset = (($today->timestamp - $this->first->month()->timestamp) / $this->seconds);
                $left = round($offset * $this->options['cellwidth']) + round(($this->options['cellwidth'] / 2) - 1);

                if ($today->timestamp > $this->first->month()->firstDay()->timestamp && $today->timestamp < $this->last->month()->lastDay()->timestamp) {
                    $html[] = '<time style="top: ' . ($this->options['cellheight'] * 2) . 'px; left: ' . $left . 'px" datetime="' . $today->format('Y-m-d') . '">Today</time>';
                }
            }

            // end data section
            $html[] = '</section>';

            // end diagram
            $html[] = '</figure>';

            return implode('', $html);
        }

        function __toString() {
            return $this->render();
        }

    }
    